//db.config.js
let mysql = require("mysql");
let config = require("./config.json");
let pool = mysql.createPool({
  connectionLimit: 10,
  host: config.host,
  user: config.user,
  port: config.port,
  password: config.password,
  database: config.database,
  timezone: 'Asia/Shanghai'
});

let connect = () => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      !err ? resolve(connection) : reject(err);
    });
  });
};

// 封装，需要传入sql语句和参数，
const query = (sql, params) => {
  return new Promise(async (resolve, reject) => {
    let connection = await connect();
    connection.query(sql, params, (err, results, fields) => {
      !err ? resolve(results) : reject(err);
      connection.release();
    });
  });
};

module.exports = {
  connect,
  query,
};

